package comm.parsers;

import java.util.ArrayList;

import org.xml.sax.Attributes;
import org.xml.sax.helpers.DefaultHandler;

import comm.util.SoptHashMap;

public class DaumSearchSAXParserHandler extends DefaultHandler {
	private StringBuffer title = new StringBuffer();
	  private StringBuffer link = new StringBuffer();
	  private StringBuffer description = new StringBuffer();
	  private StringBuffer img_url = new StringBuffer();
	  private StringBuffer image = new StringBuffer();
	  private StringBuffer target_small_url = new StringBuffer();
	  private StringBuffer author = new StringBuffer();
	  private StringBuffer answercount = new StringBuffer();
	  private StringBuffer pubdate = new StringBuffer();
	  private StringBuffer imgPubdate = new StringBuffer();
	  private StringBuffer thumbnail = new StringBuffer();
	  private StringBuffer width = new StringBuffer();
	  private StringBuffer height = new StringBuffer();
	  private StringBuffer tag = new StringBuffer();
	  private StringBuffer cpname = new StringBuffer();
	  private StringBuffer playcnt = new StringBuffer();
	  private StringBuffer playtime = new StringBuffer();
	  private StringBuffer score = new StringBuffer();
	  private StringBuffer player_url = new StringBuffer();
	  
	  
	  private boolean isTitle = false;
	  private boolean isLink = false;
	  private boolean isDescription = false;
	  private boolean isTarget_small_url = false;
	  private boolean isAuthor = false;
	  private boolean isAnswercount = false;
	  private boolean isPubdate = false;
	  private boolean isThumnail = false;
	  private boolean isWidth = false;
	  private boolean isHeight = false;
	  private boolean isTag = false;
	  private boolean isCpname = false;
	  private boolean isPlaycnt = false;
	  private boolean isPlaytime = false;
	  private boolean isScore = false;
	  private boolean isPlayer_url = false;
	  private boolean isImg_url = false;
	  private boolean isImage = false;
	  
	  private boolean isTotalcount = false;
	  private boolean isImgPubdate = false;
	 
	  private String currentBuffer = "";
	  private String totalcount = "";
	  
	  public int itemNum=1;
	  
	  
	  public ArrayList itemList = new ArrayList();
	  
	 
	  public ArrayList getItemList(){
		  return itemList;
	  }
	  
	  public void startElement(String uri, String localName,
	                     String qName, Attributes atts)
	  {
	      if (qName.equals("title")) {
	    	  isTitle=true;  title.setLength(0);
	      }else if (qName.equals("link")) {
	    	  isLink=true;  link.setLength(0); 
	      }else if (qName.equals("description")) {
	    	  isDescription=true;  description.setLength(0); 
	      }else if (qName.equals("author")) {
	    	  isAuthor=true;  author.setLength(0); 
	      }else if (qName.equals("answerCount")) {
	    	  isAnswercount=true;  answercount.setLength(0); 
	      }else if (qName.equals("pubdate")) {
	    	  isPubdate=true;  pubdate.setLength(0); 
	      }else if (qName.equals("pubDate")) {
	    	  isImgPubdate=true;  imgPubdate.setLength(0); 
	      }else if( qName.equals("thumbnail")){
	    	  isThumnail=true; thumbnail.setLength(0);
	      }else if( qName.equals("width")){
	    	  isWidth=true; width.setLength(0);
	      }else if( qName.equals("height")){
	    	  isHeight=true; height.setLength(0);
	      }else if( qName.equals("tag")){
	    	  isTag=true; tag.setLength(0);
	      }else if( qName.equals("cpname")){
	    	  isCpname=true; cpname.setLength(0);
	      }else if( qName.equals("playcnt")){
	    	  isPlaycnt=true; playcnt.setLength(0);
	      }else if( qName.equals("playtime")){
	    	  isPlaytime=true; playtime.setLength(0);
	      }else if( qName.equals("score")){
	    	  isScore=true; score.setLength(0);
	      }else if( qName.equals("image")){
	    	  isImage=true; image.setLength(0);
	      }else if( qName.equals("player_url")){
	    	  isPlayer_url=true; player_url.setLength(0);
	      }else if( qName.equals("url")){
	    	  System.out.println("====================="+qName);
	    	  isImg_url=true; img_url.setLength(0);
	      }else if( qName.equals("totalCount")){
	    	  isTotalcount=true;
	      }
	  }	  
	  
	  public void endElement(String uri, String localName,
	                     String qName)
	 { 
		  currentBuffer = "";
		   
		  if (qName.equals("item")){
	          //저장!~
			  SoptHashMap tempHm = new SoptHashMap();
			  
			  tempHm.put("totalcount",totalcount);
			  tempHm.put("title", title.toString());
			  tempHm.put("link", link.toString());
			  tempHm.put("description", description.toString());
			  tempHm.put("img_url",img_url.toString());
			  tempHm.put("image",image.toString());
			  tempHm.put("author",author.toString());
			  tempHm.put("pubdate",pubdate.toString());
			  tempHm.put("imgPubdate",imgPubdate.toString());
			  tempHm.put("thumbnail",thumbnail.toString());
			  tempHm.put("width",width.toString());
			  tempHm.put("height",height.toString());
			  tempHm.put("tag",tag.toString());
			  tempHm.put("cpname",cpname.toString());
			  tempHm.put("playcnt",playcnt.toString());
			  tempHm.put("playtime",playtime.toString());
			  tempHm.put("score",score.toString());
			  tempHm.put("player_url",player_url.toString());
			  
			  itemList.add(tempHm);
			  
	          itemNum++;
	      }else if (isTitle) {
	    	  isTitle=false; 
	      }else if (isLink) {
	    	  isLink=false;
	      }else if (isDescription) {
	    	  isDescription=false;
	      }else if (isAuthor) {
	    	  isAuthor=false;
	      }else if (isAnswercount) {
	    	  isAnswercount=false;
	      }else if (isPubdate) {
	    	  isPubdate=false;
	      }else if (isImgPubdate) {
	    	  isImgPubdate=false; 
	      }else if( isThumnail){
	    	  isThumnail=false;
	      }else if( isWidth ){
	    	  isWidth=false; 
	      }else if( isHeight){
	    	  isHeight=false; 
	      }else if( isTag ){
	    	  isTag=false;
	      }else if( isCpname ){
	    	  isCpname=false;
	      }else if( isPlaycnt ){
	    	  isPlaycnt=false;
	      }else if( isPlaytime ){
	    	  isPlaytime=false;
	      }else if( isScore ){
	    	  isScore=false;
	      }else if( isPlayer_url ){
	    	  isPlayer_url=false;
	      }else if( isImg_url){
	    	  isImg_url=false;
	      }else if( isImage){
	    	  isImage=false;
	      }else if(isTotalcount){
	    	  isTotalcount=false;
	      }
	  }
	  public void characters(char[] chars, int start, int leng) 
	  { 
		  String temp = new String(chars, start, leng);
//		  System.out.println(temp);
//		  System.out.println("######################################");
	      if (isTitle) {
	    	  currentBuffer = "title";
	    	  isTitle=false; title.append(chars,start,leng);
	      }else if (isLink) {
	    	  currentBuffer = "link";
	    	  isLink=false;  link.append(chars,start,leng);
	      }else if (isDescription) {
	    	  currentBuffer = "description";
	    	  isDescription=false;  description.append(chars,start,leng);
	      }else if (isAuthor) {
	    	  isAuthor=false;  author.append(chars,start,leng);
	      }else if (isAnswercount) {
	    	  isAnswercount=false;  answercount.append(chars,start,leng);
	      }else if (isPubdate) {
	    	  isPubdate=false;  pubdate.append(chars,start,leng);
	      }else if (isImgPubdate) {
	    	  isImgPubdate=false; imgPubdate.append(chars,start,leng);
	      }else if (isThumnail) {
	    	  isThumnail=false;  thumbnail.append(chars,start,leng);
	      }else if( isWidth ){
	    	  isWidth=false; width.append(chars,start,leng);
	      }else if( isHeight){
	    	  isHeight=false; height.append(chars,start,leng);
	      }else if( isTag ){
	    	  isTag=false; tag.append(chars,start,leng);
	      }else if( isCpname ){
	    	  isCpname=false; cpname.append(chars,start,leng);
	      }else if( isPlaycnt ){
	    	  isPlaycnt=false; playcnt.append(chars,start,leng);
	      }else if( isPlaytime ){
	    	  isPlaytime=false; playtime.append(chars,start,leng);
	      }else if( isScore ){
	    	  isScore=false; score.append(chars,start,leng);
	      }else if( isPlayer_url ){
	    	  isPlayer_url=false; player_url.append(chars,start,leng);
	      }else if( isImg_url){
	    	  currentBuffer = "url";
	    	  isImg_url=false; img_url.append(chars,start,leng);
	      }else if( isImage){
	    	  currentBuffer = "image";
	    	  isImage=false; image.append(chars,start,leng);
	      }else if(isTotalcount){
	    	  isTotalcount=false; totalcount=temp;
	      }else{ //<b>태그 문제 해결 부분- currentBuffer를 통해 < 태그 이후에 글씨들을 받아서 append
	    	  
	    	  if(currentBuffer.equals("title"))
	    		 title.append(temp);
	    	  else if(currentBuffer.equals("description"))
	    		  description.append(temp);
	    	  else if(currentBuffer.equals("link"))
	    		  link.append(temp);
	    	  else if(currentBuffer.equals("url"))
	    		  img_url.append(temp);
	    	  else if(currentBuffer.equals("image"))
	    		  image.append(temp);
	      }
	  }
}
